package com.itmiaojie.controller;

import com.itmiaojie.pojo.ResponseResult;
import com.itmiaojie.pojo.Tag;
import com.itmiaojie.pojo.dto.TagDto;
import com.itmiaojie.service.CategoryService;
import com.itmiaojie.service.TagService;
import com.itmiaojie.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/content/tag")
public class TagController {
    @Autowired
    private TagService tagService;


    /**
     * http://localhost:81/dev-api/content/tag/list?pageNum=1&pageSize=10&name=123
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, TagDto tagDto){
        return tagService.pageTagList(pageNum,pageSize,tagDto);
    }

    /**
     * http://localhost:81/dev-api/content/tag/4
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getTag(@PathVariable Long id){
        return ResponseResult.okResult(tagService.getById(id));
    }

    /**
     * {"id":7,"name":"c#","remark":"c++++"}
     * @param tagDto
     * @return
     */
    @PutMapping
    public ResponseResult update(@RequestBody TagDto tagDto){
        System.err.println(tagDto);
        return ResponseResult.okResult(tagService.updateById(BeanCopyUtils.copyBean(tagDto, Tag.class)));
    }
    @PostMapping
    public ResponseResult insert(@RequestBody TagDto tagDto){
        return ResponseResult.okResult(tagService.save(BeanCopyUtils.copyBean(tagDto, Tag.class)));
    }
    @DeleteMapping("/{id}")
    public ResponseResult delTag(@PathVariable Long id){
        return ResponseResult.okResult(tagService.removeById(id));
    }
    @GetMapping("/listAllTag")
    public ResponseResult allTag(){
        return ResponseResult.okResult(tagService.list());
    }
}
